﻿using System;

namespace DNT.RavenQA.Core.Models
{
    public class Answer : Contribute
    {
        public String QuestionId { get; private set; }

        protected Answer() { }

        protected Answer(IAnswerInput input, Func<DateTime> now)
        {
            Body = input.Body;
            User = new DenormalizedReference<User>
                       {
                          Id = input.Author.Id,
                          Name = input.Author.Name
                      };
            input.Author.IncrementContributionsCount();
            PostedOn = now();
            QuestionId = input.Question.Id;
        }

        protected Answer(IAnswerInput input) 
            : this(input, () => DateTime.UtcNow) { }

        public static Answer New(IAnswerInput input, Func<DateTime> now)
        {
            return new Answer(input, now);
        }

        public static Answer New(IAnswerInput input)
        {
            return New(input, () => DateTime.UtcNow);
        }

        public Boolean Accepted { get; private set; }

        public void MarkAsAccepted()
        {
            if (Accepted)
                throw new AnswerAlreadyAcceptedException();

            Accepted = true;
        }
    }
}